﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using TSqlServer;

/// <summary>
/// TCMSArticle 的摘要说明
/// </summary>
public class TCMSArticle : TData
{
    //常量
    const string CTableName = "t_cms_article";
    const string CKeyName = "articleid";
    //与数据表字段对应的变量
    private string FArticleID;
    private string FColumns;
    private string FTitle;
    private string FSubTitle;
    private string FKeywords;
    private string FForeword;
    private string FAContent;
    private DateTime FIssue_time;
    private DateTime FInure_time;
    private DateTime FAbort_time;
    private string FAuthor;
    private string FSource;
    private string FPublisher;
    private string FUserID;
    private int FPV;
    private int FStatus;
    private int FALevel;
    private decimal FGrade;
    private string FChknotice;
    private DateTime FOrderbyTime;
    private decimal FOrderbyNum;
    private string FRelations;
    private int FHtmlStatus;
    private string FHtmlFileName;
    private int FHtmlPages;
    private string FPageTitles;
    private int FIId;

    //分页显示使用
    public System.Data.SqlClient.SqlDataReader RResults;
    public int RowCounts;
    public int PageCounts;
    const int PageSize = 20;

    public TCMSArticle()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TCMSArticle(string sID)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = sID;
    }

    //根据ArticleID获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FArticleID = oDB["articleid"].ToString();
            FColumns = oDB["columns"].ToString();
            FTitle = oDB["title"].ToString();
            FSubTitle = oDB["subtitle"].ToString();
            FKeywords = oDB["keywords"].ToString();
            FForeword = oDB["foreword"].ToString();
            FAContent = oDB["acontent"].ToString();
            FIssue_time = oDB["issue_time"].ToDateTime();
            FInure_time = oDB["inure_time"].ToDateTime();
            FAbort_time = oDB["abort_time"].ToDateTime();
            FAuthor = oDB["author"].ToString();
            FSource = oDB["source"].ToString();
            FPublisher = oDB["publisher"].ToString();
            FUserID = oDB["userid"].ToString();
            FPV = oDB["pv"].ToInt();
            FStatus = oDB["status"].ToInt();
            FALevel = oDB["alevel"].ToInt();
            FGrade = oDB["grade"].ToDecimal();
            FChknotice = oDB["chknotice"].ToString();
            FOrderbyTime = oDB["orderbytime"].ToDateTime();
            FOrderbyNum = oDB["orderbynum"].ToDecimal();
            FRelations = oDB["relations"].ToString();
            FHtmlStatus = oDB["htmlstatus"].ToInt();
            FHtmlFileName = oDB["htmlfilename"].ToString();
            FHtmlPages = oDB["htmlpages"].ToInt();
            FPageTitles = oDB["pagetitles"].ToString();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    /// <summary>
    /// （静态）文章点击数自增。
    /// </summary>
    public static void IncPV(string sID)
    {
        string strSql = "update " + CTableName + " set pv=pv+1 where " + CKeyName + "='" + sID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    public void GetNewID()
    {
        TSqlDBProc oProc = new TSqlDBProc("ms_getnewid", 0);
        FArticleID = oProc.ms_GetNewID();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FArticleID + "','" + FColumns + "','" + FTitle + "','" + FSubTitle + "','" + FKeywords + "','" + FForeword + "','" + FAContent + "',";
        ExSql += "'" + FIssue_time.ToString() + "','" + FInure_time.ToString() + "','" + FAbort_time.ToString() + "','" + FAuthor + "','" + FSource + "','" + FPublisher + "',";
        ExSql += "'" + FUserID + "'," + FPV.ToString() + "," + FStatus.ToString() + "," + FALevel.ToString() + "," + FGrade.ToString() + ",";
        ExSql += "'" + FChknotice + "',getdate(),-100,'" + FRelations + "'," + FHtmlStatus.ToString() + ",'" + FHtmlFileName + "'," + FHtmlPages.ToString() + ",'" + FPageTitles + "'";
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "columns='" + FColumns + "',title='" + FTitle + "',subtitle='" + FSubTitle + "',keywords='" + FKeywords + "',forword='" + FForeword + "',acontent='" + FAContent + "',";
        ExSql += "issue_time='" + FIssue_time.ToString() + "',inure_time='" + FInure_time.ToString() + "',abort_time='" + FAbort_time.ToString() + "',author='" + FAuthor + "',source='" + FSource + "',publisher='" + FPublisher + "',";
        ExSql += "userid='" + FUserID + "',pv=" + FPV.ToString() + ",status=" + FStatus.ToString() + ",grade=" + FGrade.ToString() + ",alevel=" + FALevel.ToString() + ",";
        ExSql += "chknotice='" + FChknotice + "',relations='" + FRelations + "',htmlstatus=" + FHtmlStatus.ToString() + ",htmlfilename='" + FHtmlFileName + "',htmlpages=" + FHtmlPages.ToString() + ",pagetitles='" + FPageTitles + "'";
        base.Update(ExSql);
    }

    //更新记录(标题和内容)。
    public void Update_simple()
    {
        string ExSql = "title='" + FTitle + "',acontent='" + FAContent + "',issue_time=getdate()";
        base.Update(ExSql);
    }

    //更新记录(状态)。
    public void Update_status()
    {
        string ExSql = "status=" + FStatus.ToString(); ;
        base.Update(ExSql);
    }

    //更新记录(产生静态页的状态)。
    public void Update_htmlstatus()
    {
        string ExSql = "htmlstatus=1";
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string sID)
    {
        TData.DeleteByID(sID, CTableName, CKeyName);
    }

    //更新orderbytime
    public static void UpdateOrderbyTime(string sID)
    {
        string strSql = "update " + CTableName + " set orderbytime=getdate() where " + CKeyName + "='" + sID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    //更新orderbynum
    public static void UpdateOrderbyNum(string sID, int iNum)
    {
        string strSql = "update " + CTableName + " set orderbynum=" + iNum.ToString() + " where " + CKeyName + "='" + sID + "'";
        TSqlDB.ExecuteNonQuery(strSql);
    }

    //计算页数
    public void CountPages()
    {
        Regex r = new Regex("-page-");
        string[] ContentPage = r.Split(FAContent);
        FHtmlPages = ContentPage.Length;
    }

    //分页显示
    public void GetPage(int PageNum)
    {
        int iStartRow, iStopRow;

        iStartRow = (PageNum - 1) * PageSize + 1;
        iStopRow = PageNum * PageSize;

        TSqlDBProc oProc = new TSqlDBProc("ms_GetNewsPage", 0);
        oProc.ms_GetNewsPage(iStartRow, iStopRow);

        RResults = oProc.FDataReader;
    }

    //生成pagebar
    public string GenPagebar(int PageNum)
    {
        string result = "<div class=\"pagebox\">";

        CountPage();

        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getnews('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getnews('2')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getnews('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getnews('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getnews('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getnews('" + (PageNum - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getnews('" + (PageNum + 1).ToString() + "')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getnews('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getnews('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getnews('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getnews('" + (PageCounts - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        //result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + PageSize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }

    //计算可分页数
    private void CountPage()
    {
        string sSql = "select count(*) from t_cms_article";
        RowCounts = TSqlDB.ExecuteScalar(sSql);

        if (RowCounts % PageSize == 0)  //余数为零
        {
            PageCounts = RowCounts / PageSize;
        }
        else
        {
            PageCounts = (int)(RowCounts / PageSize) + 1;
        }
    }


    public string ArticleID
    {
        get
        {
            return FArticleID;
        }
        set
        {
            FArticleID = value;
        }
    }

    public string Columns
    {
        get
        {
            return FColumns;
        }
        set
        {
            FColumns = value;
        }
    }

    public string Title
    {
        get
        {
            return FTitle;
        }
        set
        {
            FTitle = value;
        }
    }

    public string SubTitle
    {
        get
        {
            return FSubTitle;
        }
        set
        {
            FSubTitle = value;
        }
    }

    public string Keywords
    {
        get
        {
            return FKeywords;
        }
        set
        {
            FKeywords = value;
        }
    }

    public string Foreword
    {
        get
        {
            return FForeword;
        }
        set
        {
            FForeword = value;
        }
    }

    public string AContent
    {
        get
        {
            return FAContent;
        }
        set
        {
            FAContent = value;
        }
    }

    public DateTime Issue_time
    {
        get
        {
            return FIssue_time;
        }
        set
        {
            FIssue_time = value;
        }
    }

    public DateTime Inure_time
    {
        get
        {
            return FInure_time;
        }
        set
        {
            FInure_time = value;
        }
    }

    public DateTime Abort_time
    {
        get
        {
            return FAbort_time;
        }
        set
        {
            FAbort_time = value;
        }
    }

    public string Author
    {
        get
        {
            return FAuthor;
        }
        set
        {
            FAuthor = value;
        }
    }

    public string Source
    {
        get
        {
            return FSource;
        }
        set
        {
            FSource = value;
        }
    }

    public string Publisher
    {
        get
        {
            return FPublisher;
        }
        set
        {
            FPublisher = value;
        }
    }

    public string UserID
    {
        get
        {
            return FUserID;
        }
        set
        {
            FUserID = value;
        }
    }

    public int PV
    {
        get
        {
            return FPV;
        }
        set
        {
            FPV = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public int ALevel
    {
        get
        {
            return FALevel;
        }
        set
        {
            FALevel = value;
        }
    }

    public decimal Grade
    {
        get
        {
            return FGrade;
        }
        set
        {
            FGrade = value;
        }
    }

    public string Chknotice
    {
        get
        {
            return FChknotice;
        }
        set
        {
            FChknotice = value;
        }
    }

    public DateTime OrderbyTime
    {
        get
        {
            return FOrderbyTime;
        }
        set
        {
            FOrderbyTime = value;
        }
    }

    public decimal OrderbyNum
    {
        get
        {
            return FOrderbyNum;
        }
        set
        {
            FOrderbyNum = value;
        }
    }

    public string Relations
    {
        get
        {
            return FRelations;
        }
        set
        {
            FRelations = value;
        }
    }

    public int HtmlStatus
    {
        get
        {
            return FHtmlStatus;
        }
        set
        {
            FHtmlStatus = value;
        }
    }

    public string HtmlFileName
    {
        get
        {
            return FHtmlFileName;
        }
        set
        {
            FHtmlFileName = value;
        }
    }

    public int HtmlPages
    {
        get
        {
            return FHtmlPages;
        }
        set
        {
            FHtmlPages = value;
        }
    }

    public string PageTitles
    {
        get
        {
            return FPageTitles;
        }
        set
        {
            FPageTitles = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}
